import math
import time
import os
import sys

# 将工程目录加入包扫描
currPath = os.path.abspath(os.path.dirname(__file__))
srcPath = os.path.split(currPath)[0]
rootPath = os.path.split(srcPath)[0]
projectPath = os.path.split(rootPath)[0]
sys.path.append(projectPath)

from src.db.CcxDataCenterDb import CcxDataCenter
from src.db.ProdDb import ProdDb
from src.utils.LogUtils import Logger

logger = Logger()


# 同步其他QE级别数据到fits(银行、券商)
class syncOther:
    def __init__(self, logger, localDb, dataCenterDb):
        self.logger = logger
        self.localDb = localDb
        self.dataCenterDb = dataCenterDb

    def __queryQeIndex__(self):
        sql = """
        SELECT * FROM qe_result WHERE release_status = 1 AND `status` = 1 AND ver = 0
        """
        return self.dataCenterDb.Query(sql)

    def __deleteQeIndex__(self):
        sql = """
        DELETE from qe_result
        """
        return self.localDb.Query(sql, None)

    def __save_db__(self, data):
        regionSql = """
        
        INSERT INTO `qe_result` (`id`, `code`, `credit_code`, `level`, `score`, `rating_result`, `create_time`, `type_code`, `score_type`, `year`, `message`,
         `param`, `sign`, `release_status`, `last_release_time`, `last_release_user`, `last_edit_time`, `last_edit_user`, `ver`, `status`) VALUES 
         (
            %(id)s,
          %(code)s,
          %(credit_code)s,
          %(level)s,
          %(score)s,
          %(rating_result)s,
          %(create_time)s,
          %(type_code)s,
          %(score_type)s,
          %(year)s,
          %(message)s,
          %(param)s,
          %(sign)s,
          %(release_status)s,
          %(last_release_time)s,
          %(last_release_user)s,
          %(last_edit_time)s,
          %(last_edit_user)s,
          %(ver)s,
          %(status)s
          )
        """

        temp = []
        for i in data:
            data = {
                "id": i["id"],
                "code": i["code"],
                "credit_code": i["credit_code"],
                "level": i["level"],
                "score": i["score"],
                "rating_result": i["rating_result"],
                "create_time": i["create_time"],
                "type_code": i["type_code"],
                "score_type": i["score_type"],
                "year": i["year"],
                "message": i["message"],
                "param": i["param"],
                "sign": i["sign"],
                "release_status": i["release_status"],
                "last_release_time": i["last_release_time"],
                "last_release_user": i["last_release_user"],
                "last_edit_time": i["last_edit_time"],
                "last_edit_user": i["last_edit_user"],
                "ver": i["ver"],
                "status": i["status"],
            }
            temp.append(data)
        logger.info("数据一共%s条" % len(temp))
        self.localDb.ExecMany(regionSql, temp, 1000)


if __name__ == "__main__":
    logger = Logger()
    localDb = ProdDb(logSql=False, autocommit=True)
    dataCenterDb = CcxDataCenter(logSql=False, autocommit=True)
    t = time.perf_counter()
    logger.info("========同步qe数据 开始 ========")
    syncData = syncOther(logger=logger, localDb=localDb, dataCenterDb=dataCenterDb)
    # 删除数据
    syncData.__deleteQeIndex__()
    data = syncData.__queryQeIndex__()
    syncData.__save_db__(data)
    cast = (time.perf_counter() - t)
    m = math.floor(cast / 60)
    s = cast - m * 60
    logger.info('总耗时: %d分%.3f秒' % (m, s))
    logger.info("========同步qe数据 结束 ========")
